package com.gjintel.business.webcontent.dao.Impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.gjintel.business.webcontent.dao.IWebContentDao;
import com.gjintel.business.webcontent.dto.Combo;
import com.gjintel.business.webcontent.model.WebContent;
import com.gjintel.framework.register.model.RegisterUser;
import com.gjintel.sdk.base.dao.HibernateGenericDAO;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;
import com.gjintel.sdk.util.StringUtil;


/**
 * @description 文章类型数据处理实现
 * @author Ant-K
 * @date 2016-3-16 下午19:38:39
 */
@SuppressWarnings("all")
@Repository("WebContentDao")
public class WebContentDaoImpl extends HibernateGenericDAO implements IWebContentDao{

//	@SuppressWarnings("unchecked")
//	public List<ArticleContent> findAll() {
//		String hql = "from ArticleContent g where g.flag=0  order by g.dis_sort asc";
//		return this.find(hql);
//	}
	
	@SuppressWarnings("unchecked")
	public List<WebContent> findAll() {
		String hql = " select new WebContent(g.id,g.type,t.name,g.content,g.title,g.disSort,g.isDisplay,g.createTime,g.updateTime,g.createrId,g.updaterId,g.createrName,g.updaterName,g.flag)"
				+" from WebContent g,WebTypeInfo t where g.type=t.id and g.flag=0 and t.flag=0 order by g.disSort asc";
		return this.find(hql);
	}
	
	@SuppressWarnings("unchecked")
	public List<WebContent> findAll(String Id) {
		String hql = " select new WebContent(g.id,g.type,t.name,g.content,g.title,g.disSort,g.isDisplay,g.createTime,g.updateTime,g.createrId,g.updaterId,g.createrName,g.updaterName,g.exploreUserId,g.flag,g.endShowTime)"
				+" from WebContent g,WebTypeInfo t where g.type=t.id and g.id='"+Id+"' and g.flag=0 and t.flag=0 order by g.disSort asc";
		return this.find(hql);
	}
	
	@SuppressWarnings("unchecked")
	public List<WebContent> findAllByType(String type,Page page,String deptId) {
		List params = new ArrayList();
		//StringBuffer hql = new StringBuffer("select new WebContent(g.id,g.title,g.createTime) from WebContent g where g.flag=0 and g.isDisplay=0");
		//StringBuffer hql = new StringBuffer("select new WebContent(g.id,g.title,g.createTime,pd.name) from WebContent g,Dept d,Dept pd where pd.id=d.pUnit and d.id=g.deptId and g.flag=0 and g.isDisplay=0");
		StringBuffer hql = new StringBuffer("select new WebContent(g.id,g.title,g.createTime,d.pUnit) from WebContent g,Dept d where d.id=g.deptId and g.flag=0 and g.isDisplay=0");
		
		if(!StringUtil.isEmpty(type)){
			hql.append(" and g.type= ? ");
			params.add(type);
		}
		if(!StringUtil.isEmpty(deptId) && !"--请选择--".equals(deptId)){
			hql.append(" and d.pUnit= ? ");
			params.add(deptId.trim());
		}
		hql.append(" order by g.createTime desc,g.disSort asc");
		return this.findAsPage(hql.toString(), page,params.toArray());
	}
	
	@SuppressWarnings("unchecked")
	public void findAll(Page page, WebContent articlecontent, Sort sort,java.sql.Date startTime,java.sql.Date endTime,String deptId,Boolean noEditPubType) {
		List params = new ArrayList();
//		StringBuffer hql = new StringBuffer("select new WebContent(g.id,g.type,t.name,g.content,g.title,g.disSort,g.isDisplay," +
//		"g.createTime,g.updateTime,g.createrId,g.updaterId,g.createrName,g.updaterName,g.projectId)"
//		+" from WebContent g,WebTypeInfo t,Dept d where g.type=t.id and d.id=g.deptId and g.flag=0 and t.flag=0");
//		StringBuffer hql = new StringBuffer("from WebContent g where g.flag=0 ");
		StringBuffer hql = new StringBuffer("select new WebContent(g.id,g.type,t.name,g.content,g.title,g.disSort,g.isDisplay," +
		"g.createTime,g.updateTime,g.createrId,g.updaterId,g.createrName,g.updaterName,g.projectId)"
		+" from WebContent g,WebTypeInfo t,Dept d where g.type=t.id and d.id=g.deptId and g.flag=0 and t.flag=0");
		if (articlecontent != null) {
			if (!StringUtil.isEmpty(articlecontent.getTitle())) {
				hql.append(" and g.title like ? ");
				params.add("%" + articlecontent.getTitle().trim() + "%");
			}
			if (!StringUtil.isEmpty(articlecontent.getType()) && !"--请选择类型--".equals(articlecontent.getType())) {
				hql.append(" and g.type=? ");
				params.add(articlecontent.getType().trim());
			}
			if (!StringUtil.isEmpty(articlecontent.getProjectId())) {
				hql.append(" and g.projectId like ? ");
				params.add(articlecontent.getProjectId().trim());
			}
		}
		if(startTime != null){
			hql.append(" and g.createTime > ?");
			params.add(startTime);
		}
		if(endTime != null){
			hql.append(" and g.createTime < ?");
			params.add(endTime);
		}
		if(noEditPubType!=null&&noEditPubType){
			hql.append(" and g.type !=34 ");//变更公告
		}
//		if(sort!=null){
//			sort.setFields("disSort");
//			sort.setOrders("asc");
//		}
		this.findAsPage(hql.toString(), page, sort, "g", false, params.toArray());
	}
	@SuppressWarnings("unchecked")
	public void findAll(Page page, WebContent articlecontent) {
		StringBuffer hql = new StringBuffer("select new WebContent(g.id,g.type,t.name,g.content,g.title,g.disSort,g.isDisplay," +
				"g.createTime,g.updateTime,g.createrId,g.updaterId,g.createrName,g.updaterName,g.flag,d.name)"
				+" from WebContent g,WebTypeInfo t,Dept d where g.type=t.id and d.id=g.deptId and g.flag=0 and t.flag=0");
		List params = new ArrayList();
		
		hql.append(" order by g.disSort asc");
		this.findAsPage(hql.toString(), page, params.toArray());
	}
//	@SuppressWarnings("unchecked")
//	public void findAll(Page page, ArticleContent articlecontent, Sort sort) {
//		StringBuffer hql = new StringBuffer("from ArticleContent g where g.flag=0");
//		List<String> params = new ArrayList<String>(2);
//		if (articlecontent != null) {
//			if (!StringUtil.isEmpty(articlecontent.getTitle())) {
//				hql.append(" and g.title like ? ");
//				params.add("%" + articlecontent.getTitle().trim() + "%");
//			}
//			if (!StringUtil.isEmpty(articlecontent.getType())) {
//				hql.append(" and g.type=? ");
//				params.add(articlecontent.getType().trim());
//			}
//		}
//		hql.append(" order by g.dis_sort asc");
//		this.findAsPage(hql.toString(), page, sort, "g", false, params.toArray());
//	}

	@Override
	public List<WebContent> findListByProjectId(String projectId) {
		String hql="select new WebContent(g.id,g.type,t.name,g.content,g.title,g.disSort,g.isDisplay,g.createTime,g.updateTime,g.createrId,g.updaterId,g.createrName,g.updaterName,g.flag)"
				+" from WebContent g,WebTypeInfo t where g.type=t.id and g.flag=0 and t.flag=0 and g.projectId=? order by g.disSort asc";// and t.isDisplay=2
		return this.find(hql, projectId);
	}

	@Override
	public List<WebContent> findListByType(WebContent webContent) {
		String hql = "from WebContent w where  w.type = :name ";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql).setParameter("name", webContent.getType());
		query.setFirstResult(0); 
		query.setMaxResults(8); 
		return query.list(); 
		
	}

	@Override
	public List<WebContent> getUnAutoNotice(String type) {
		String hql = "select wc from WebContent wc,WebTypeInfo wt  where  wc.type = wt.id and wc.flag=0 and wt.isDisplay=1 and wt.id=? order by wc.createTime desc";	
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, type);
		query.setFirstResult(0); 
		query.setMaxResults(8); 
		return query.list(); 
	}

	@Override
	public List<WebContent> findOppugnWebContentList(Page page, WebContent webcontent) {
		String hql="select new WebContent(g.id,g.type,t.name,g.content,g.title,g.disSort,g.isDisplay,"
		 +"g.createTime,g.updateTime,g.createrId,g.updaterId,g.createrName,g.updaterName,g.endShowTime)"
		+" from WebContent g,WebTypeInfo t,Dept d  where g.type=t.id and d.id=g.deptId and g.flag=0 and t.flag=0 and g.isDisplay!=1 "
		+" and g.projectId=? and g.type in('21','22','23','24')";
		return this.findAsPage(hql.toString(), page,webcontent.getProjectId());
	}

	@Override
	public void findAllJoinProject(Page page, WebContent webcontent) {
		List params = new ArrayList();
		StringBuffer hql = new StringBuffer("select new WebContent(g.id,g.type,t.name,g.content,g.title,g.disSort,g.isDisplay," +
				"g.createTime,g.updateTime,g.createrId,g.updaterId,g.createrName,g.updaterName,g.endShowTime)"
				+" from WebContent g,WebTypeInfo t,Dept d  where g.type=t.id and d.id=g.deptId and g.flag=0 and t.flag=0 and g.isDisplay!=1 "
				+ " ");
		if (webcontent != null) {
			if (!StringUtil.isEmpty(webcontent.getTitle())) {
				hql.append(" and g.title like ? ");
				params.add("%" + webcontent.getTitle().trim() + "%");
			}
			if (!StringUtil.isEmpty(webcontent.getType())) {
				hql.append(" and g.type=? ");
				params.add(webcontent.getType().trim());
			}
//			if (!StringUtil.isEmpty(webcontent.getProjectId())) {
//				hql.append(" and g.projectId like ? ");
//				params.add(webcontent.getProjectId().trim());
//			}
		}
		
		if(webcontent.getType().equals("32")//中标公示
				||webcontent.getType().equals("33")//流标公示
				||webcontent.getType().equals("34")//变更公告
				||webcontent.getType().equals("41")//合同公示
				||webcontent.getType().equals("42")//验收报告公示
				||webcontent.getType().equals("31")){//竞价公示
			hql.append(" order by g.endShowTime desc");
		}else {
			hql.append(" order by g.createTime desc");
		}
		this.findAsPage(hql.toString(),page, params.toArray());
	}

	@Override
	public List<WebContent> findWebByBusinessId(String businessId) {
		String hql = "from WebContent w where w.exploreUserId=? and w.flag=0 order by w.createTime desc";
		return this.find(hql, businessId);
	}

	@Override
	public List<RegisterUser> findAllComboList() {
		String hql = "from RegisterUser r where r.removeFlag!=1 and  r.flag=1";
		return this.find(hql);
	}
	
	
}
